/*
 * 
 * WordPres版微信小程序
 * author: jianbo
 * organization: 守望轩  www.watch-life.net
 * github:    https://github.com/iamxjb/winxin-app-watch-life.net
 * 技术支持微信号：iamxjb
 * 开源协议：MIT
 * Copyright (c) 2017 https://www.watch-life.net All rights reserved.
 */

var util = require('util.js');
var wxApi = require('wxApi.js')
var wxRequest = require('wxRequest.js')
var Api = require('api.js');
var app = getApp();
module.exports = {
  //获取用户信息和openid
  getUsreInfo: function () {
    var self = this;       
    wx.showLoading({
      mask: true
    });  
    var wxLogin = wxApi.wxLogin();
    var jscode = '';
    wxLogin().then(response => {
      jscode = response.code
      var wxGetUserInfo = wxApi.wxGetUserInfo()
      return wxGetUserInfo()
    }).
    //获取用户信息
    then(response => {
      console.log(response.userInfo);
      console.log("成功获取用户信息(公开信息)");
      app.globalData.userInfo = response.userInfo;
      app.globalData.isGetUserInfo = true;
      var url = Api.getOpenidUrl();
      var data = {
        code: jscode,
        encrypted_data: response.encryptedData,
        iv: response.iv,
        raw_data: response.rawData,
        signature: response.signature
      }
      var postOpenidRequest = wxRequest.postRequest(url, data);
      //获取openid
      postOpenidRequest.then(response => {
        if (response.data.code == '200') {
          console.log("openid 获取成功");
          app.globalData.openid = response.data.data.openid;
          app.globalData.uid = response.data.data.uid;
          wx.setStorageSync('token', response.data.data.token);
          app.globalData.isGetOpenid = true;
        }
        else {
          console.log(response);
        }
        wx.hideLoading();
      })               
    }).catch(function (error) {
      console.log('error: ' + error.errMsg);
      wx.hideLoading();
      self.userAuthorization();
    })
  },
  userAuthorization: function () {
    var self = this;
    // 判断是否是第一次授权，非第一次授权且授权失败则进行提醒
    wx.getSetting({
      success: function success(res) {
        console.log(res.authSetting);
        var authSetting = res.authSetting;
        if (util.isEmptyObject(authSetting)) {
          console.log('第一次授权');
        } else {
          console.log('不是第一次授权', authSetting);
          // 没有授权的提醒
          if (authSetting['scope.userInfo'] === false) {
            wx.showModal({
              title: '用户未授权',
              content: '如需正常使用评论、点赞、发布等功能需授权获取用户信息。是否在授权管理中选中“用户信息”?',
              showCancel: true,
              cancelColor: '#296fd0',
              confirmColor: '#296fd0',
              confirmText: '设置权限',
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                  wx.openSetting({
                    success: function success(res) {
                      console.log('打开设置', res.authSetting);
                      var scopeUserInfo = res.authSetting["scope.userInfo"];
                      if (scopeUserInfo) {
                        self.getUsreInfo();
                      }
                    }
                  });
                }
              }
            })
          }
        }
      }
    });
  }
}